package com.regvision.contacts.ui.adapter;

import java.util.List;

import com.regvision.contacts.R;
import com.regvision.contacts.domain.EMailModel;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class EMailAdapter extends BaseAdapter {

	private Context mContext;
	private int resource;
	private List<EMailModel> list;
	
	public EMailAdapter(Context context, int resource, List<EMailModel> list) {
		this.mContext = context;
		this.resource = resource;
		this.list = list;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Holder holder;
		EMailModel model = (EMailModel)getItem(position);
		
		if (convertView == null) {
			convertView = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(resource, null);
			holder = new Holder();
			
			holder.tvName = (TextView) convertView.findViewById(R.id.tv_item_email_name);
			holder.tvMail = (TextView) convertView.findViewById(R.id.tv_item_email_mail);
			
			convertView.setTag(holder);
		} else {
			holder = (Holder)convertView.getTag();
		}

		holder.tvName.setText(model.label + "");
		holder.tvMail.setText(model.address);
		
		return convertView;
	}	
	
	
	public void updateView(List<EMailModel> list) {
		this.list = list;
		this.notifyDataSetChanged();
	}
	
	class Holder {
		public TextView tvName;
		public TextView tvMail;
	}

}
